package ebusiness.ebusiness.Controllers;

import ebusiness.ebusiness.Dao.UserDao.UserQueryVO;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;
import ebusiness.ebusiness.Entity.UserEntity.UserDetail;
import ebusiness.ebusiness.Mapper.PathUtil.PagePath;
import ebusiness.ebusiness.Mapper.PathUtil.WebPath;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;

@RestController
public class RegisteController {
    @RequestMapping(path = WebPath.REGISTER,method = RequestMethod.POST)
    public String register(String name, String password)throws Exception{


        // 创建UserMapper对象
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		// 由sqlsession创建mapper的代理对象
//		UserMapper mapper = sqlSession.getMapper(UserMapper.class);

//        //获取时间
//        Date date = new Date();
//        Timestamp customer_createtime = new Timestamp(date.getTime());
    
        RegisteUser customer = new RegisteUser();
        customer.setName(name);
        customer.setPassword(password);
//        customer.setCustomer_createtime(customer_createtime);
        UserQueryVO userQueryVO = new UserQueryVO();
        RegisteUser user = userQueryVO.getRegisteUserByName(name);
        if(user != null){
            throw new Exception("username exist!");
        }else{
            try {
                userQueryVO.addARegisteUser(customer);
            }catch (Exception e){
                throw e;
            }
            return "1";
        }
        
    }
    @RequestMapping(path = WebPath.ModifyPASSWORD,method = RequestMethod.POST)
    public String modifyPassword(HttpSession session, String password, String newPassword ) throws Exception{
        UserQueryVO userQueryVO = new UserQueryVO();
        int id = (int) session.getAttribute("user");

        RegisteUser user= userQueryVO.getRegisteUserByID(id);
        String respassword = user.getPassword();

//			 MessageDigest md = MessageDigest.getInstance("MD5");
//	         byte[] inputData = password.getBytes();
//	         md.update(inputData);
//	         BigInteger bigInteger = new BigInteger(md.digest());
//			 password = bigInteger.toString(32);

        if (password.equals(respassword)) {
            try {
                userQueryVO.modifyPassWord(id,newPassword);
            }catch (Exception e){
                return "0";
            }
            return "1";
        } else {
            //密码错误
            return "0";
        }
    }

    @RequestMapping(path = WebPath.FINDPASSWORD,method = RequestMethod.POST)
    public String findPassword(HttpSession session, String newpassword) throws Exception{
        UserQueryVO userQueryVO = new UserQueryVO();
        int id = (int) session.getAttribute("user");
        try {
            RegisteUser registeUser = new RegisteUser();
            registeUser.setId(id);
            registeUser.setPassword(newpassword);
            userQueryVO.modifyPassWord(id,newpassword);
        }catch (Exception e){
            throw e;
        }
        return "1";
    }
}
